package com.ohos.carbon.slice.componentList;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.window.dialog.IDialog;
import ohos.agp.window.dialog.ListDialog;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class RegistrationFromSlice extends AbilitySlice {
    private DirectionalLayout directionalLayout;
    private Button button;
    private Image back_button;
    private Text text_title, text;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_registration_from);

        //ListDialog
        directionalLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_directionalLayout);
        button = (Button) findComponentById(ResourceTable.Id_button);
        directionalLayout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showListDialog();
            }
        });

        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Registration from")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Registration from")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Registration from", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Registration from", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Registration from", 1);
            pf.flushSync();
        }
    }

    private void showListDialog() {
        ListDialog dlg = new ListDialog(this);
        String[] items = {"Male", "Female"};
        dlg.setItems(items);
        dlg.setOnSingleSelectListener(new IDialog.ClickedListener() {
            @Override
            public void onClick(IDialog iDialog, int i) {
                /*new ToastDialog(getContext())
                        .setText(items[i])
                        .show();*/
                switch (i) {
                    case 0:
                        button.setText("Male");
                        dlg.destroy();
                        break;
                    case 1:
                        button.setText("Female");
                        dlg.destroy();
                        break;

                }

            }
        });
        dlg.show();
        //dlg.getListContainer().setPadding(50, 10, 10, 0);
    }


    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Registration from");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
